﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Collections;

namespace Fastyou.Other.Consoles.LINQ
{
    public class Linq2XML
    {
        public void CreateXml()
        {
            XDocument doc = new XDocument
                (
                new XComment("this is test linq 2 xml"),
                new XElement
                    ("Books",
                    new XElement
                        ("Book",
                        new XElement("Title", "Book Title1"),
                        new XElement("Price", "$23.434")
                        ),
                    new XElement
                        ("Book",
                        new XElement("Title", "Book Title2"),
                        new XElement("Price", "$4964.54")
                        )

                    ),
                new XComment("end")
                );
            doc.Declaration = new XDeclaration("1.0", "utf-8", "true");
            doc.Save(@"C:\test.xml");

        }

        public void CreateXmlElement()
        {
            XDocument xdoc = new XDocument(CreateElement());
            xdoc.Declaration = new XDeclaration("1.0", "utf-8", "true");
            xdoc.Save(@"C:\test1.xml");
        }

        public void CreateLinqXmlElement()
        {
            XDocument xdoc = new XDocument(CreateLinqElement());
            xdoc.Declaration = new XDeclaration("1.0", "utf-8", "true");
            xdoc.Save(@"C:\test2.xml");
        }

        /// <summary>
        /// TO DO
        /// </summary>
        public void GetAttributes()
        {
            XElement doc = XElement.Load(@"c:\test1.xml");
            IEnumerable xt = from xml in doc.Elements("Contact")
                             where (string)xml.Attribute("Type") == "C2"

                             //from inxml in xml.Element("Name")
                             //where (string)inxml.Attribute("Name") == "laihua"
                             select xml;

            foreach (XElement el in xt)
                Console.WriteLine(el);

        }

        private XElement CreateElement()
        {
            XElement el = new XElement(
                new XElement("Contacts",
                    new XElement("Contact",
                        new XElement("Name", "sky.lai"),
                        new XElement("Phone", "2w3423"),
                        new XElement("Address",
                            new XElement("Street", "street1"),
                            new XElement("city", "sz")
                            )
                            ),
                    new XElement("Contact",
                        new XElement("Name", "laihua "),
                        new XElement("Phone", "aaaaaaaa"),
                        new XElement("Address",
                            new XElement("Street", "street2"),
                            new XElement("city", "gz")
                            )
                            )
                            )
                            );
            return el;
                        
        }

        private XElement CreateLinqElement()
        {
            XElement srcTree = new XElement("Root",
                new XElement("Element", 1),
                new XElement("Element", 2),
                new XElement("Element", 3),
                new XElement("Element", 4),
                new XElement("Element", 5)
              );

            XElement xmlTree = new XElement("Root",
                new XElement("Child", 1),
                new XElement("Child", 2),
                from el in srcTree.Elements()
                where (int)el > 2
                select el
            );

            return xmlTree;
        }
    }
}
